﻿Imports EHaskins.Frc.Dashboard
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Public Class DashboardController
    Implements INotifyPropertyChanged
    Implements IDashboardController

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Protected Sub RaisePropertyChanged(ByVal prop As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(prop))
    End Sub

    Dim _processors As ObservableCollection(Of IUserDataProcessor)
    Dim _currentProcessor As IUserDataProcessor
    Dim _client As IDashboardClient(Of IFrcDashUserDataProcessor)

    ''' <summary>
    ''' Initializes a new instance of the DashboardController class.
    ''' </summary>
    ''' <param name="client"></param>
    Public Sub New(ByVal client As IDashboardClient(Of IFrcDashUserDataProcessor))
        _client = client
        _processors = New ObservableCollection(Of IUserDataProcessor)
        _processors.Add(New DefaultUserDataProcessor())
        _currentProcessor = Nothing

        AddHandler _client.UserData.InvalidateCurrentProcessor, AddressOf Me.FindProcessorEvent
        AddHandler _client.UserData.NewUserDataReceived, AddressOf Me.ProcessData
    End Sub


    Private Sub FindProcessor()
        Dim origin = _client.UserData.CurrentProcessorOriginID
        Dim id = _client.UserData.CurrentProcessorID

        FindProcessor(origin, id)
    End Sub

    Private Sub FindProcessor(ByVal origin As Integer, ByVal id As Integer)
        Dim processors = From p In Me.Processors Where p.ProcessorOriginId = origin And p.ProcessorId = id

        If processors.Count = 1 Then
            CurrentProcessor = processors(0)
        ElseIf processors.Count = 0 Then
            CurrentProcessor = Nothing
        Else
            Throw New InvalidOperationException("Cannot have multiple user data processors with the same OriginID, " & origin & " and ID, " & id & ".")
        End If
    End Sub

    Private Sub FindProcessorEvent(ByVal sender As Object, ByVal e As EventArgs)
        Try
            FindProcessor()
        Catch ex As Exception
            Throw
        End Try
    End Sub
    Private Sub ProcessData(ByVal sender As Object, ByVal e As EventArgs)
        If CurrentProcessor IsNot Nothing Then
            CurrentProcessor.ShallowParse(_client.UserData.UserData)
        Else
            FindProcessor()
        End If
    End Sub

    Public Property Processors() As ObservableCollection(Of IUserDataProcessor) Implements IDashboardController.Processors
        Get
            Return _processors
        End Get
        Set(ByVal value As ObservableCollection(Of IUserDataProcessor))
            If _processors Is value Then
                Return
            End If
            _processors = value
            RaisePropertyChanged("Processors")
        End Set
    End Property
    Public Property CurrentProcessor() As IUserDataProcessor Implements IDashboardController.CurrentProcessor
        Get
            Return _currentProcessor
        End Get
        Set(ByVal value As IUserDataProcessor)
            If _currentProcessor Is value Then
                Return
            End If
            _currentProcessor = value
            RaisePropertyChanged("CurrentProcessor")
        End Set
    End Property
    Public Property Client() As IDashboardClient Implements IDashboardController.Client
        Get
            Return _client
        End Get
        Set(ByVal value As IDashboardClient)
            If _client Is value Then
                Return
            End If
            _client = value
            RaisePropertyChanged("Client")
        End Set
    End Property
End Class
